var client = {};
var app = getApp();

// 收藏页修改收藏列表顺序接口
client.upCollectionOrder = function (houseIdList, that) {
  my.request({
    url: app.globalData.url + "3b782a6f-ba01-4c0b-b832-941d1a37bf2b",
    method: "POST",
    data: {
      houseIdList: houseIdList
    },
    headers: {
      "Access-Token": app.globalData.token
    },
    // 期望返回的数据格式，默认json，支持json，text，base64
    dataType: "json",
    // 调用成功的回调函数
    success: function (res) {
      if (res.data.succeed == 0) {
        client.getCollectionList(that);
      } else {
        app.errorHandling("调整顺序失败：", res);
      }
    },
    // 调用失败的回调函数
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () { }
  });
};

// 收藏页获取收藏列表接口
client.getCollectionList = function (that) {
  my.request({
    url: app.globalData.url + "e652f551-abc4-4a41-833c-cdef43c93506",
    method: "POST",
    headers: {
      "Access-Token": app.globalData.token
    },
    // 期望返回的数据格式，默认json，支持json，text，base64
    dataType: "json",
    // 调用成功的回调函数
    success: function (res) {
      console.log("getCollectionList  " + JSON.stringify(res));
      if (res.data.succeed == 0) {
        var collectionList = res.data.data;
        var taskId = my.getStorageSync({ key: 'taskId' }).data;
        var buyerStatus = my.getStorageSync({ key: 'buyerStatus' + taskId }).data;
        for (var j = 0; j < collectionList.length; j++) {
          collectionList[j].idLocal = j + 1;
          if (j == 0) {
            collectionList[j].upBackground = "rgba(255,255,255,1)";
          }
          // 放弃，认购，或结束。则背景颜色置灰
          if (buyerStatus == 2 || buyerStatus == 3 || buyerStatus == 4) {
            collectionList[j].background = "#DEDEDE";
          } else {
            // 0 没人选购，1-已经锁定，2-已经被认购
            if (collectionList[j].status == "0") {
              collectionList[j].background = "rgba(246,142,51,1)";
            } else if (collectionList[j].status == "1") {
              collectionList[j].background = "rgba(255,116,116,1)";
            } else {
              collectionList[j].background = "rgba(153,153,153,1)";
            }
          }
        }
        that.setData({
          collectionList: collectionList
        });
      } else {
        app.errorHandling("收藏页获取收藏列表失败：", res);
      }
    },
    // 调用失败的回调函数
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () { }
  });
};

// 收藏列表页删除收藏接口
client.deleteCollection = function (houseId, that) {
  console.log("houseId: " + houseId);
  my.request({
    url: app.globalData.url + "c11ce533-2b21-4e86-bec1-3646d3c6d636",
    method: "POST",
    data: {
      houseId: houseId
    },
    headers: {
      "Access-Token": app.globalData.token
    },
    // 期望返回的数据格式，默认json，支持json，text，base64
    dataType: "json",
    // 调用成功的回调函数
    success: function (res) {
      console.log("deleteCollection  " + JSON.stringify(res));
      if (res.data.succeed == 0) {
        my.showToast({
          content: "删除收藏成功",
          duration: 2000
        });
        client.getCollectionList(that);
      } else {
        app.errorHandling("删除收藏失败：", res);
      }
    },
    // 调用失败的回调函数
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () { }
  });
};

module.exports = {
  client: client
};